<?php

namespace app\stores\controller\user;

use think\Controller;

class Address extends Controller{
    public function __construct()
    {
        parent::__construct();

        $this->UserModel=model('User.User');
        $this->AddressModel=model('User.Address');
        $this->RegionModel=model('Region');
    }

    public function index(){
        if($this->request->isPost()){
            $userid=$this->request->param('userid',0);

            $user=$this->UserModel->find($userid);
            if(!$user){
                $this->error('用户不存在');
                exit;
            }
            $list=$this->AddressModel->with(['provinces','citys','districts'])->where(['userid'=>$userid])->select();
            $this->success('返回收货地址成功',null,$list);
            exit;
        }
    }

    public function add(){
        if($this->request->isPost()){
            $params=$this->request->param();

            $userid=$this->request->param('userid',0);
            $code=$this->request->param('code',0);

            $user=$this->UserModel->find($userid);
            if(!$user){
                $this->error('用户不存在');
                exit;
            }

            $data=[
                'consignee'=>$params['consignee'],
                'mobile'=>$params['mobile'],
                'address'=>$params['address'],
                'status'=>$params['status'],
                'userid'=>$params['userid'],
            ];

            $parentpath=$this->RegionModel->where(['code'=>$code])->value('parentpath');

            if(empty($parentpath)){
                $this->error('您所选择的地区不存在，请重新选择');
                exit;
            }

            $path=explode(',',$parentpath);
            if(count($path)<=0){
                $this->error('您所选择的地区不存在，请重新选择');
                exit;
            }

            list($data['province'],$data['city'],$data['district'])=$path;

            if($data['status']==1){
                $UpdateStatus=$this->AddressModel->where(['userid'=>$userid])->update(['status'=>0]);
                if($UpdateStatus === FALSE){
                    $this->error('更新默认收货地址失败');
                    exit;
                }
            }

            $result=$this->AddressModel->validate('common/User/Address')->save($data);

            if($result === FALSE){
                $this->error($this->AddressModel->getError());
                exit;
            }else{
                $this->success('添加收货地址成功');
                exit;
            }
        }
    }

    public function edit(){
        if($this->request->isPost()){
            $params=$this->request->param();

            // 获取用户id 判断用户是否存在
            $userid=$this->request->param('userid',0);
            $addrid=$this->request->param('addrid',0);
            $code=$this->request->param('code',0);

            $user=$this->UserModel->find($userid);
            if(!$user){
                $this->error('用户不存在');
                exit;
            }
            $address=$this->AddressModel->find($addrid);
            if(!$address){
                $this->error('收货地址不存在');
                exit;
            }

            if($address['userid'] != $userid){
                $this->error('非法修改他人地址，无权修改');
                exit;
            }

            $data=[
                'id'=>$addrid,
                'consignee'=>$params['consignee'],
                'mobile'=>$params['mobile'],
                'address'=>$params['address'],
                'status'=>$params['status'],
                'userid'=>$params['userid'],
            ];

            $parentpath=$this->RegionModel->where(['code'=>$code])->value('parentpath');

            if(empty($parentpath)){
                $this->error('您所选择的地区不存在,请重新选择');
                exit;
            }

            $path=explode(',',$parentpath);

            if(count($path) <= 0){
                $this->error('您所选择的地区不存在,请重新选择');
                exit;
            }

            list($data['province'],$data['city'],$data['district'])=$path;

            if($data['status']==1){
                $UpdateStatus=$this->AddressModel->where(['userid'=>$userid])->update(['status'=>0]);
                if($UpdateStatus===FALSE){
                    $this->error('更新默认收货地址失败');
                    exit;
                }
            }

            $result=$this->AddressModel->validate('common/User/Address')->isUpdate(true)->save($data);
            if($result===FALSE){
                $this->error($this->AddressModel->getError());
                exit;
            }else{
                $this->success('更新收货地址成功');
                exit;
            }
        }
    }

    public function info(){
        if($this->request->isPost()){
            $addrid=$this->request->param('addrid',0);
            $userid=$this->request->param('userid',0);

            $user=$this->UserModel->find($userid);

            if(!$user){
                $this->error('用户不存在');
                exit;
            }

            $address=$this->AddressModel->with(['provinces','citys','districts'])->find($addrid);
            if(!$address){
                $this->error('收货地址不存在');
                exit;
            }
            
            if($address['userid'] != $userid){
                $this->error('非法修改他人地址，无权修改');
                exit;
            }

            $this->success('返回收货地址成功',null,$address);
            exit;
        }
    }

    public function check(){
        if($this->request->isPost()){
            $userid=$this->request->param('userid',0);
            $addrid=$this->request->param('address',0);

            $user=$this->UserModel->find($userid);
            if(!$user){
                $this->error('用户不存在');
                exit;
            }
            
            $address=$this->AddressModel->where(['id'=>$addrid])->find();

            if(!$address){
                $this->error('收货地址不存在');
                exit;
            }

            if($address['userid'] != $userid){
                $this->error('非法修改他人地址，无权修改');
                exit;
            }
            
            $this->AddressModel->startTrans();

            $UpdateStatus=$this->AddressModel->where(['userid'=>$userid])->update(['status'=>0]);
            if($UpdateStatus===FALSE){
                $this->error('更新默认收货地址失败');
                exit;
            }

            $result=$this->AddressModel->where(['id'=>$addrid])->update(['status'=>1]);
            if($result===FALSE){
                $this->AddressModel->rollback();
                $this->error('更新默认地址失败');
                exit;
            }else{
                $this->AddressModel->commit();
                $this->success('更新默认地址成功');
                exit;
            }
        }
    }

    public function del(){
        if($this->request->isPost()){
            $userid=$this->request->param('userid',0);
            $addrid=$this->request->param('addrid',0);

            $user=$this->UserModel->find($userid);
            if(!$user){
                $this->error('用户不存在');
                exit;
            }

            $address=$this->AddressModel->find($addrid);
            if(!$address){
                $this->error('收货地址不存在');
                exit;
            }

            if($address['userid'] != $userid){
                $this->error('非法修改他人地址，无权修改');
                exit;
            }

            $result=$this->AddressModel->destroy($addrid);

            if($result===FALSE){
                $this->error('删除失败');
                exit;
            }else{
                $this->success('删除成功');
                exit;
            }
        }
    }

    public function order(){
        if($this->request->isPost()){
            $userid=$this->request->param('userid');

            $user=$this->UserModel->find($userid);
            if(!$user){
                $this->error('用户不存在');
                exit;
            }

            $where=[
                'userid'=>$userid,
                'status'=>1
            ];

            $address=$this->AddressModel->with(['provinces','citys','districts'])->where($where)->find();
            if($address){
                $this->success('返回默认收货地址',null,$address);
                exit;
            }

            $info=$this->AddressModel->with(['provinces','citys','districts'])->where(['userid'=>$userid])->find();
            if(!$info){
                $this->error('暂无收货地址');
                exit;
            }
            $this->success('返回默认收货地址',null,$info);
            exit;
        }
    }
}